<?php

/**
 * @Description    系统配置
 * @Author         zhaojiping
 * @Copyright      Copyright (c) 山东艾思软件科技有限公司(https://www.aisisofit.cn)
 * @Date           2022/1/20
 * @CreateBy       VS Code
 */

namespace app\core\model;

use think\Model;

class CoreConfig extends \app\common\model\CommonModel {
    protected $fields = 'id, name, type, title, group, describe, value, extral, sort'; // 通用查询字段
    protected $order = 'sort, id'; // 排序字段

    public static function onBeforeInsert($row) {
        parent::onBeforeInsert($row);

        cache('config', null);
        cache('init_config', null);
    }
    public static function onBeforeUpdate($row) {
        parent::onBeforeUpdate($row);

        cache('config', null);
        cache('init_config', null);
    }
    public static function onBeforeDelete($row) {
        parent::onBeforeDelete($row);

        cache('config', null);
        cache('init_config', null);
    }

    /**
     * 根据分组获取配置列表
     * @param $group
     */
    public function getConfigListByGroup($group) {
        $where = [];
        if (!empty($group)) {
            $where['group'] = $group;
        }
        $where['status'] = 1;
        $order = 'sort';
        $list = self::where($where)->order($order)->select();

        foreach ($list as &$item) {
            if ($item['type'] == 4) {
                $item['extra_list'] = parse_config_attr($item['extra']);
            }
        }
        return $list;
    }
}
